<?php
/**
 * Created by PhpStorm.
 * User: cowthan
 * Date: 2018/7/8
 * Time: 下午10:48
 */

class MyUtils
{

    public static function getCurrentUser(\Illuminate\Http\Request $request){
        $sid = $request->header('sid');
        if($sid == null) $sid = $request->input('sid');
        $user = \MyDB::getInstance()->findOne("select * from admins where sid=:sid", ['sid' => $sid]);

//        echo json_encode($user);
        return $user;

    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return SafeAssoc
     */
    public static function extractInput(\Illuminate\Http\Request $request){
        $data = $request->input();

        $assoc = SafeAssoc::create();
        if($data == null) return $assoc;

        foreach ($data as $name => $value){
            $assoc->put($name, $value);
        }

        return $assoc;
    }

    public static function extractHeader(\Illuminate\Http\Request $request){
        $data = $request->header();

        $assoc = SafeAssoc::create();
        if($data == null) return $assoc;

        foreach ($data as $name => $value){
            $assoc->put($name, $value);
        }

        return $assoc;
    }

    public static function extractFlash(\Illuminate\Http\Request $request){
        $data = $request->flash();

        $assoc = SafeAssoc::create();
        if($data == null) return $assoc;

        foreach ($data as $name => $value){
            $assoc->put($name, $value);
        }

        return $assoc;
    }

}